#!/usr/bin/python3

import requests
import sys
from prettytable import PrettyTable#打印数据比较美观

HEADERS = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0"}


def argvclean():
    a = sys.argv[1:]
    if a == [] or a == ['-h']:
        help()
    elif ("-f" in a) and len(a) == 2 :
        f = getHtml(a[a.index('-f')+1])
        if f == 0:
            print("程序异常!!")
        else :
            table = []
            x = PrettyTable(["ID","企业名称","许可证编号","发证机关","有效期至","发证日期"])
            for j in f:
                for i in j:
                    table.append([i['ID'],i['EPS_NAME'],i['PRODUCT_SN'],i['QF_MANAGER_NAME'],i['XK_DATE'],i['XK_DATE']])
            for i in table:
                x.add_row(i)
            print(x)
            
    elif ("-i" in a) and len(a) == 2 :
        #print(a[a.index('-i')+1])
        status = getHtml2(a[a.index('-i')+1])
        if status == 0:
            print("程序异常  或  id 错误!!")    
        else :
            print(status)
    else:
        help()

def help():
    print('''本程序可以找到某个化妆瓶公司的 基本信息
-h      查看帮助
-f      查找某个化妆品公司,默认只显示第一页,也就是15条最佳匹配项
-i      通过ID完整查询

example: 
yaopin -f 北京 
yaopin -i f8852fbf9c75489696d7f357a79f5434
''')


def getHtml(name):
    data = {'on': 'true', 
            'page': '1', 
            'pageSize': '15', 
            'productName': name, 
            'conditionType': '2', 
            'applyname': '', 
            'applysn': ''
            }
    global HEADERS
    try :
        r = requests.post("http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList",data = data, headers=HEADERS)
        r.raise_for_status()
        json_r = r.json()
        a = []
        for i in range(2,int(json_r["pageCount"])+1):
            da = {'on': 'true', 
            'page': str(i), 
            'pageSize': '15', 
            'productName': name, 
            'conditionType': '2', 
            'applyname': '', 
            'applysn': ''
            }
            r = requests.post("http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsList",data = data, headers=HEADERS)
            r.raise_for_status()
            a.append(r.json()['list'])
        return a#list
    except :
        return 0

def getHtml2(id):#o选项
    url= "http://125.35.6.84:81/xk/itownet/portalAction.do?method=getXkzsById"
    data = {"id":id}
    global HEADERS
    try :
        r = requests.post(url,headers = HEADERS, data = data)
        # print(id)
        r.raise_for_status()
        # print(456)
        a = r.json()
        #print(a)
        if a['epsName'] == "":
            raise
        list_name = [
                    '企业名称：',
                    "许可证编号：",
                    "许可项目：",
                    "企业住所：",
                    "生产地址：",
                    "社会信用代码：",
                    "法定代表人：",
                    "企业负责人：",
                    "质量负责人：",
                    "发证机关：",
                    "签发人：",
                    "日常监督管理机构：",
                    "日常监督管理人员：",
                    "有效期至：",
                    "发证日期：",
                    "投诉举报电话："
                    ]
        x = PrettyTable()#后面按照列来添加,要不太费劲
        x.add_column("信息",list_name)
        list_a = [
            a['epsName'],
            a['productSn'],
            a['certStr'],
            a['epsAddress'],
            a['epsProductAddress'],
            a['businessLicenseNumber'],
            a['legalPerson'],
            a['businessPerson'],
            a["qualityPerson"],
            a["qfManagerName"],
            a["xkName"],
            a["rcManagerDepartName"],
            a["rcManagerUser"],
            a["xkDate"],
            a["xkDateStr"],
            '12331',
        ]
        x.add_column("企业信息",list_a)
        return x
    except :
        return 0


if __name__ == "__main__":
    argvclean()
    
#Anthor:江户川_柯北
#fileCreatedDate2020-7-29-12-32
#lastChange:2020-7-29-12-32